package payroll.webapp

class DesignationController extends AuthController {

    def add = {
		if (request.method == 'GET') {
			def designation = new Designation()
			def salary = new BaseSalary()
			
			[designation : designation,
			 salary		 : salary]
		}
		else {
			def designation = new Designation(params['designation'])
			def salary = new BaseSalary(params['salary'])
			
			designation.baseSalary = salary
			
			if (designation.save()) {
				redirect(action:'list')
			}
			else {
				render view:'add', model:[designation : designation,
										  salary	  : salary]
			}
		}
	}
	
	def edit = {
		def designation = Designation.get(params.id)
		def salary = designation.baseSalary
		def map = [designation : designation,
				   salary	   : salary]
		
		if(request.method == 'GET') {
			render view:'add', model:map
		}
		else
		{
			designation.properties = params['designation']
			salary.properties = params['salary']
			
			if (designation.save()) {
				redirect(action:'list')
			}
			else {
				render view:'add', model:map
			}
		}
	}
	
	def list = {
		[designations: Designation.list()]
	}
	
	def delete = {
		def designation = Designation.get(params.id)
		
		if (designation) {
			designation.delete()
			redirect(action:'list')
		}
	}
}
